package com.net.tkc.pro.controller;

import com.net.tkc.pro.entity.Follow;
import com.net.tkc.pro.entity.Friends;
import com.net.tkc.pro.entity.ResultEntity;
import com.net.tkc.pro.impl.FollowImpl;
import com.net.tkc.pro.impl.FriendsImpl;
import com.net.tkc.pro.tool.JsonUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@CrossOrigin //解决跨域问题
@RestController
@RequestMapping("/Friends/")
public class FriendsController {
    ResultEntity resultEntity = null;
    @Resource
    FriendsImpl friendsImpl;

    @GetMapping("GetList")
    private String getList(
    ){
        resultEntity = new ResultEntity();
        try{
            List<Friends> list=friendsImpl.getList();
            if (list.size()!=0){
                resultEntity.setCount(list.size());
                resultEntity.setCode("0");
                resultEntity.setData(list);
                resultEntity.setMsg("获取成功！");
            }else{
                resultEntity.setMsg("获取失败,请查看是否有数据");
                resultEntity.setCode("2");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setMsg("获取失败");
            resultEntity.setCode("1");
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }

    /**
     * 通过friendId查找好友
     * @param userid 被关注人的id
     * @return
     */
    @GetMapping("GetFriend")
    private String getFriendList(
            @RequestParam("userid") int  userid
    ){
        resultEntity = new ResultEntity();
        try{
            List<Friends> list=friendsImpl.getFriendList(userid);
            if (list.size()!=0){
                resultEntity.setCount(list.size());
                resultEntity.setCode("0");
                resultEntity.setData(list);
                resultEntity.setMsg("获取成功！");
            }else{
                resultEntity.setMsg("获取失败,请查看是否有数据");
                resultEntity.setCode("2");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setMsg("获取失败");
            resultEntity.setCode("1");
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }

    /**
     * 用friendid查找好友
     * @param friendid 关注的人id
     * @return
     */
    @GetMapping("GetUser")
    private String getUserList(
            @RequestParam("friendid") int friendid
    ){
        resultEntity = new ResultEntity();
        try{
            List<Friends> list=friendsImpl.getUserList(friendid);
            if (list.size()!=0){
                resultEntity.setCount(list.size());
                resultEntity.setCode("0");
                resultEntity.setData(list);
                resultEntity.setMsg("获取成功！");
            }else{
                resultEntity.setMsg("获取失败,请查看是否有数据");
                resultEntity.setCode("2");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setMsg("获取失败");
            resultEntity.setCode("1");
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }

    /**
     * 取消好友
     * @param status 状态
     * @param id id
     * @return
     */
    @PutMapping("Friends")
    private String upFriends(
            @RequestParam("status") int status,
            @RequestParam("id") int id
    ){
        resultEntity = new ResultEntity();
        try{
            int result = friendsImpl.upFriends(status,id);
            if(result>0){
                resultEntity.setCode("0");
                resultEntity.setMsg("取消好友成功！");
            }else{
                resultEntity.setCode("1");
                resultEntity.setMsg("取消好友失败！");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg(e.getMessage());
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }

    /**
     * 添加好友
     * @param userid 用户1
     * @param friendid 用户2
     * @return
     */
    @PostMapping("Friends")
    private String addFriends(
            @RequestParam("userid") int userid,
            @RequestParam("friendid") int friendid
    ){
        resultEntity = new ResultEntity();
        Friends friends=new Friends();
        DateFormat dateFormat=new SimpleDateFormat("yyyy/MM/dd HH:mm");
        Date date=new Date();
        String Time=dateFormat.format(date);
        friends.setDttime(Time);
        friends.setUserid(userid);
        friends.setFriendid(friendid);
        try{
            int result = friendsImpl.addFriends(friends);
            if(result>0){
                resultEntity.setCode("0");
                resultEntity.setMsg("添加成功！");
            }else{
                resultEntity.setCode("1");
                resultEntity.setMsg("添加失败！");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg(e.getMessage());
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }
}
